/**
 * 房間數據處理類
 * 功能分別為：初始、獲取、設置
 */
function roomVars() {
	
}

/**
 * 設置服務端房間屬性
 * @param r_room	房間對象
 * @param r_param	房間屬性對象｛name:value｝,支持Array,Object类型变量
 */
roomVars.prototype.setProListVar = function (r_room,r_param) {	
		for ( var v in r_param ) {				
			r_room.properties.put(v, r_param[v] );		
		}		
}

/**
 * 設置服務端房間屬性
 * @param r_room	房間對象
 * @param r_var	房間屬性名
 * @param r_val 值,支持Array,Object类型变量
 */
roomVars.prototype.setProVar = function (r_room, r_var, r_val) {	
	r_room.properties.put(r_var, r_val );	
}
/**
 * 获取房间属性
 */
roomVars.prototype.getProVar = function (r_room,r_var) {
	var _res = null;
	if(r_room){
		_res = r_room.properties.get(r_var);
	}else {
		__debug.trace("##err: roomVars::setProVar() , r_room = " +r_room );
	}
	return _res;
}

/**
 * 移除房间属性
 */
roomVars.prototype.removeProVar = function (r_room,r_var) {	
	if(r_room){
		_res = r_room.properties.remove(r_var);
	}else {
		__debug.trace("##err: roomVars::setProVar() , r_room = " +r_room );
	}
}

/**
 * 服務啟動初始房間
 * 並將房間id分類存入全局變量__room_list，按游戏场分类
 */
roomVars.prototype.formatRoomVariable = function() {
	var _zone = _server.getCurrentZone();
	var _rooms = _zone.getRooms();
	if (_rooms.length > 0) {
		for each(var _temp_room in _rooms) {
			_server.destroyRoom(_zone, _temp_room.getId());
		}
	}
	var _hall = {};
	var _sql = "SELECT * FROM texas_hall WHERE hall_status = '1' ";
	var _res = __conn.query(_sql);
	if (_res != null) {
		var _row ;
		var _index;
		var _room_vars = [];		
		var _race_room = 0;  //房间类型，0为普通房间，1为比赛房间
		var _counter = _res.size();
		for ( var i = 0; i < _counter; i++) {
			_row = _res.get(i);
			_index = parseInt(_row.getItem('hall_id'));//大廳游戲場類別ID
			_race_room = parseInt(_row.getItem('hall_race')); //是否是比赛房间
			_hall[_index] =  { id: _index, name:String(_row.getItem('hall_name')),race: _race_room } ; //將大廳數據存入對象，索引為游戲場id	
			
			//根据数据库数据创建游戏房间				
			if(_race_room == 0){
				_sql = "SELECT * FROM texas_room WHERE room_type = '%d'";
			}else {
				_sql = "SELECT * FROM texas_race_room WHERE room_type = '%d'";
			}
			var _room_res = __conn.query(_sql, [_index]);
			if (_room_res != null) {
				var _counter_2 = _room_res.size();
				__room_list[_index] = [];  //初始化该类型游戏场数组，用于记录该类别游戏场的所有房间ID
				for ( var j = 0; j < _counter_2; j++) {
					_row = _room_res.get(j); 					
					//设置屬性，用于创建房间
					var _room_obj = {
						name:	String(_row.getItem('room_name')),
						pwd:	String(_row.getItem('room_pass')),
						maxU:	parseInt(_row.getItem('room_max_user')),
						maxS:	parseInt(_row.getItem('room_max_sper')),
						isGame:	parseInt(_row.getItem('room_isgame')),
						isLimbo:parseInt(_row.getItem('room_islimbo')),
						uCount:	false,
						xtName:	String(_row.getItem('room_extension')),
						xtClass:String(_row.getItem('room_class'))
					};					
					//房間變量
					if ( _race_room == 1) {
						//比赛房间
						_room_vars = [						
							{ name:'race', val:1, priv:false },         //游戏场类别
							{ name:'type', val:parseInt(_row.getItem('room_type')), priv:false },        //游戏场类别							
							{ name:'buy', val:parseInt(_row.getItem("room_buy")), priv:false },    //小盲注
							{ name:'cost', val:parseInt(_row.getItem("room_cost")), priv:false },   //参赛费用
							{ name:'players', val:parseInt(_row.getItem("room_players")), priv:false }, //游戏玩家所需人数 
							{ name:'expend', val:parseInt(_row.getItem("room_expend")), priv:false },  //每局消耗 							
							{ name:'bonus1', val:parseInt(_row.getItem("room_first_bonus")), priv:false }, //第一名嬴的筹码
							{ name:'bonus2', val:parseInt(_row.getItem("room_second_bonus")), priv:false },  //第二名赢得筹码						
							{ name:'bonus3', val:parseInt(_row.getItem("room_third_bonus")), priv:false },  //第三名赢得筹码							
							{ name:'marks1', val:parseInt(_row.getItem("room_first_marks")), priv:false },   //第一名积分
							{ name:'marks2', val:parseInt(_row.getItem("room_second_marks")), priv:false },  //第二名积分
							{ name:'marks3', val:parseInt(_row.getItem("room_third_marks")), priv:false },  //第三名积分	
							{ name:'minBlind', val:parseInt(_row.getItem("room_min_blind")), priv:false },//小盲注
							{ name:'maxBlind', val:parseInt(_row.getItem("room_max_blind")), priv:false },//大盲注
							{ name:'state', val:0, priv:false },											
							];
					}else {
						   //普通游戏房间
							_room_vars = [						
							{ name:'race', val:0, priv:false },         //游戏场类别
							{ name:'type', val:parseInt(_row.getItem('room_type')), priv:false },         //游戏场类别
							{ name:'limit', val:parseInt(_row.getItem("players_limit")), priv:false }, //游戏玩家人数上限
							{ name:'minBlind', val:parseInt(_row.getItem("room_min_blind")), priv:false },//小盲注
							{ name:'maxBlind', val:parseInt(_row.getItem("room_max_blind")), priv:false },//大盲注
							{ name:'minChip', val:parseInt(_row.getItem("room_min_chip")), priv:false },  //最小携带  
							{ name:'maxChip', val:parseInt(_row.getItem("room_max_chip")), priv:false },  //最大携带 
							{ name:'marks', val:parseInt(_row.getItem("room_marks")), priv:false },   //每局积分
							{ name:'expend', val:parseInt(_row.getItem("room_expend")), priv:false },  //每局消耗				
							{ name:'state', val:0, priv:false },												
							];

					}
					//檢查房間是否存在	
					var _zone = _server.getCurrentZone();
					var _room = _zone.getRoomByName(_room_obj["name"]);
					if (_room) continue;
					//创建房间
					var _game_room = _server.createRoom(_room_obj, null, false, false, _room_vars, null, false);
					if(_game_room){
						var _room_id = _game_room.getId();	//房间ID
						__room_list[_index].push(_room_id);  //存入对应类别的对应游戏场
					}
				} //end of for											
			}			
		} //end of for		
	}
	__hall = _hall;
	
	//創建大廳房間，大廳中的玩家都加入到該房間
	_room_obj = {
					name:	"hall",
					pwd:	"",
					maxU:	10000,
					maxS:	10000,
					isGame:	0,
					isLimbo:1,
					uCount:	false,
					xtName:	"",
					xtClass:""
				};	
	__hall_room = _server.createRoom(_room_obj, null, false, false, null, null, false);	
}

/**
 * 获取房间变量值
 * @param	r_room 房间
 * @param	r_var 变量名
 * @param	r_type 返回的变量类型 int | str | bool
 */
roomVars.prototype.getVar = function(r_room, r_var, r_type) {	
	if (r_room == null) return null;
	var _var = r_room.getVariable(r_var);
	if (_var == null) return null;
	var _value = _var.value;
	switch(String(r_type)) {		
		case "str":
			return String(_value);
			break;
		case "bool":
			return Boolean(_value);
			break;		
		case "int":
			return parseInt(_value);
			break;
		default:
			return _value;
	}	
}

/**
 * @param r_room 房间
 * @param r_data 数据对象  {key: val,...}
 */
roomVars.prototype.setVar = function(r_room, r_param) {
	var _valList = [];
	for ( var v in r_param ) {
		_valList.push({ name:v, val:r_param[v], priv:false });
	}
	if ( _valList.length > 0 ) {
		_server.setRoomVariables(r_room, null, _valList, false, false);		
	}
}

/**
 * 获取房间当前状态
 * @param	r_room
 * return String    start | ready | free
 */
roomVars.prototype.getState = function(r_room,r_bool_val) {
	var _state = __roomVars.getVar(r_room, "state", "int");
	if (r_bool_val) {
		return parseInt(_state);
	}
	switch(_state) {
		case 0:
			return "free";   //房间空闲
			break;
		case 1:
			return "start";  //游戏正在进行中
			break;
		case 2:
			return "ready";  //准备开始游戏，正在进行倒计时
			break;		
	}	
}

/**
 * 
 * @param	r_room
 * @param	r_state
 */
roomVars.prototype.setState = function(r_room, r_state) {
	var _val = 0;
	switch(r_state) {
		case "free":
			_val = 0;
			break;
		case "start":
			_val = 1
			break;
		case "ready":
			_val = 2
			break;		
	}	
	__roomVars.setVar(r_room, { state : _val } );
}

/**
 * 获取房间类型
 * @return int 
 */
roomVars.prototype.getRoomType = function(r_room) {
	var _type = this.getVar(r_room, "race", "int");
	return _type;	
}







/**
 * 用戶站起或離開房間釋放他的座位 
 */
function clearSitIndex(r_room, r_sit) {
	if (r_sit == -1 || r_room == null) {
		return;
	}
	var _index_arr = r_room.properties.get("indexArr");
	var _news = r_room.getVariable("news");
	if (_index_arr != null ) {
		for (var i = 0; i < _index_arr.length; i++ ) {
			if (_index_arr[i] == r_sit) {				
				if (_news != null) {
					_news = _news.value;
					if (_news == _index_arr[i]) {
						_news = (i == (_index_arr.length - 1))?_index_arr[0]:_index_arr[i + 1];
					}
				}
				_index_arr.splice(i, 1);
				break;
			}
		}
		if (_news !=null) {
			setRoomVariable(r_room, { news:_news }, false);
		}
		r_room.properties.put("indexArr", _index_arr);
	}
}

